
package cn.xabad.commons.io.filefilter;

import cn.xabad.commons.io.FileUtils;

import java.io.File;
import java.io.Serializable;
import java.util.Date;


public class AgeFileFilter extends AbstractFileFilter implements Serializable {

    
    private final long cutoff;
    
    private final boolean acceptOlder;

    
    public AgeFileFilter(long cutoff) {
        this(cutoff, true);
    }

    
    public AgeFileFilter(long cutoff, boolean acceptOlder) {
        this.acceptOlder = acceptOlder;
        this.cutoff = cutoff;
    }

    
    public AgeFileFilter(Date cutoffDate) {
        this(cutoffDate, true);
    }

    
    public AgeFileFilter(Date cutoffDate, boolean acceptOlder) {
        this(cutoffDate.getTime(), acceptOlder);
    }

    
    public AgeFileFilter(File cutoffReference) {
        this(cutoffReference, true);
    }

    
    public AgeFileFilter(File cutoffReference, boolean acceptOlder) {
        this(cutoffReference.lastModified(), acceptOlder);
    }

    
    
    @Override
    public boolean accept(File file) {
        boolean newer = FileUtils.isFileNewer(file, cutoff);
        return acceptOlder ? !newer : newer;
    }

    
    @Override
    public String toString() {
        String condition = acceptOlder ? "<=" : ">";
        return super.toString() + "(" + condition + cutoff + ")";
    }
}
